package com.zmzncs.shop.module.shop.service.impl;

import com.zmzncs.shop.common.pojo.LoginUser;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.common.util.JwtUtil;
import com.zmzncs.shop.module.shop.mapper.ShopGoodsMapper;
import com.zmzncs.shop.module.shop.mapper.ShopManagerMapper;
import com.zmzncs.shop.module.shop.mapper.ShopTicketMapper;
import com.zmzncs.shop.module.shop.pojo.shopmanager.ShopManagerDTO;
import com.zmzncs.shop.module.shop.pojo.shopmanager.ShopManagerVO;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopinfo.*;
import com.zmzncs.shop.module.shop.mapper.ShopInfoMapper;
import com.zmzncs.shop.module.shop.service.ShopInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 任建波
 * @since 2021-05-18
 */
@Service
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> implements ShopInfoService {

    @Autowired
    ShopInfoMapper shopInfoMapper;
    @Autowired
    ShopGoodsMapper shopGoodsMapper;
    @Autowired
    ShopTicketMapper shopTicketMapper;
    @Autowired
    ShopManagerMapper shopManagerMapper;

    @Autowired
    HttpServletRequest request;

    /**
     * 分页
     */
    @Override
    public IPage<ShopInfoDTO> page(ShopInfoSearchForm form) {
        IPage<ShopInfoDTO> iPage = shopInfoMapper.list(form.getPage());

        return iPage;
    }

    /**
     * 添加
     */
    @Override
    @Transactional
    public void add(ShopInfoAddForm form) {
        ShopInfo shopInfo = new ShopInfo();
        BeanUtils.copyProperties(form, shopInfo);

        shopInfoMapper.insert(shopInfo);
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public void delete(Long id) {
        ShopInfo shopInfo = new ShopInfo();

        shopInfoMapper.updateById(shopInfo);
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public void update(ShopInfoUpdateForm form) {
        ShopInfo shopInfo = new ShopInfo();
        BeanUtils.copyProperties(form, shopInfo);

        shopInfoMapper.updateById(shopInfo);
    }

    /**
     * 详情
     */
    @Override
    public ShopInfoDTO getById(Long id) {
        ShopInfoDTO shopInfoDTO = shopInfoMapper.getById(id);

        return shopInfoDTO;
    }

    @Override
    public List<ShopInfoDTO> getByManageId() {
        LoginUser loginUser = JwtUtil.getLoginUser(request);

        List<ShopInfoDTO> infoDTOList = shopInfoMapper.getByManageId(loginUser.getManagerId());
        if (infoDTOList != null && infoDTOList.size() > 0) {
            for (ShopInfoDTO infoDTO : infoDTOList) {
                //商户商品数量
                Integer goodsCount = shopGoodsMapper.getCountByShopId(infoDTO.getId());
                infoDTO.setGoodsCount(goodsCount);
                //商户优惠券数量
                Integer ticketCount = shopTicketMapper.getCountByShopId(infoDTO.getId());
                infoDTO.setTicketCount(ticketCount);
                //商户管理员信息
                List<ShopManagerDTO> managerDTOS = shopManagerMapper.getManagerByShopId(infoDTO.getId());
                List<ShopManagerVO> managerList = JsonUtil.listTransform(managerDTOS, ShopManagerVO.class);
                infoDTO.setManagerList(managerList);
            }
        }
        return infoDTOList;
    }

}
